import { _decorator, Button, director, instantiate, Label, Node, Sprite, SpriteAtlas, View } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { DALIYCHAType, GameType, Respones, RewardType, Stype } from '../../Config/GameCfg';
import { GameApp } from '../../GameApp';
import { GameTip } from '../../../../Framework/Scripts/Managers/TipMgr';
import {IStageReward } from '../../../AssetPackage/Props/Game/DT_TZ';
import { DailyChallengeRes, GamePlayTzRes } from '../../../AssetPackage/Props/Hall/Game/GameTZ';
import { ViewMgr } from '../../../../Framework/Scripts/Managers/ViewMgr';
import { IDailyChallengeRes, IZtChange } from '../GamHall/GameHallProto';
const { ccclass, property } = _decorator;

/**
 * 休闲挑战 和 大神挑战
 */
@ccclass('GameMRTZ_TZ_Ctr')
export class GameMRTZ_TZ_Ctr extends UIComponent {
    spTitle: Sprite;
    labNum: Label;
    spBg2: Node;
    isClick:Boolean ;
    dcType: number = Respones.INVALID;;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(0);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.spTitle = this.ViewComponent('spBg1',Sprite);
        this.labNum = this.ViewComponent('labNum',Label);
        this.spBg2 = this.ViewNode('spBg2/layout');
        
        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick1);
        this.AddButtonListener('btnStart',this,this.onBntStartClick);
    }

    private InitData(){
        this.isClick = true;
        // this.dcType = Respones.INVALID;
    }

    private registerEvent(){
        // globalThis.gameMgr._eventTarget.on(EventMsgKes.loading,this.onLoadingPro,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGamePlayTzRes,this.onGamePlayTzRes,this);
    }

    private unRegisterEvent(){
        // globalThis.gameMgr._eventTarget.off(EventMsgKes.loading,this.onLoadingPro,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGamePlayTzRes,this.onGamePlayTzRes,this);
    }

    private setNum(num:number){
        this.labNum.string = `连续回答${num}道问题`;   
    }

    public setData(data:IDailyChallengeRes){
        if(data.topicTotal){
              this.setNum(data.topicTotal); 
        }

        let sf = null;
        let atals = ResManager.inst.getAsset(AbNames.GameHall,'Atals/GameHall',SpriteAtlas) as SpriteAtlas;
        if(this.hasProInObj(data,'dcType')){
            this.dcType = data.dcType;
            let str = '';
            if(data.dcType == DALIYCHAType.DC_XX){
                sf = atals.getSpriteFrame('GameMRTZ_XX_9bg');
                str = '休闲挑战';
            }else if(data.dcType == DALIYCHAType.DC_DS){
                sf = atals.getSpriteFrame('GameMRTZ_DS_9bg');
                str = '大神挑战';
            }
            if(sf){
                this.spTitle.spriteFrame = sf;
            }
        }
   
        let list = data.listReward;
        if(list){
            let children = this.spBg2.children
            let item = children[0];
            for (let i = 0; i < list.length-1; i++) {
                let node = instantiate(item);
                node.parent = this.spBg2;    
            }

            console.log('======children=======',children.length);
            for (let i = 0; i < list.length; i++) {
                const cfg:any = list[i];

                let lab = children[i].getChildByName('lab').getComponent(Label);
                let spIcon = children[i].getChildByName('icon').getComponent(Sprite);
                let labNum = children[i].getChildByName('lab2').getComponent(Label);
                let spLine = children[i].getChildByName('spLine');

                lab.string = `连续答对${cfg.topicNum}道题奖励`;
                labNum.string = `x${cfg.rewardNum}`;

                let sfIcon = null;
                if(cfg.rewardType === RewardType.Type_Quan){
                    sfIcon = atals.getSpriteFrame('GameMRTZ_icon');
                }else if(cfg.rewardType === RewardType.TYpe_QiuNum){
                    sfIcon = atals.getSpriteFrame('GameHall_top_icon');
                }
                if(sfIcon){
                    spIcon.spriteFrame = sfIcon;
                }

                if(i == list.length - 1){
                    spLine.active = false;
                }
            }
        }
    }

    private OnBtnCloseClick1(){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameMRTZ/GameMRTZ',UIType.Full); 
    }

    //开始答题
    private onBntStartClick(bnt: Button){
        if(this.isClick === false){
            return;
        }
        this.isClick = false;

        // 发送服务器请求开始答题
        if(this.dcType !== Respones.INVALID){
            globalThis.gameMgr._GameHallMsg.GamePlayTzReq(this.dcType);
        }else{
            console.error('=====请求开始每日挑战类型错误======',this.dcType);
        }
    }

    //开始答题返回
    private onGamePlayTzRes(data:GamePlayTzRes){
        this.isClick = true;
        console.log('========onStartGameRes=======');
        if(this.hasProInObj(data,'code')){
            let code = data.code;   
            if(code === Respones.OK){  
                // ViewMgr.inst.ConnectToGame(GameType.Game_TZ);

                //加载大厅资源
                SceneMgr.inst.LoadGameRes(()=>{
                    console.log('=========大厅资源加载完毕==========')
                    //加载完毕链接网络
                    ViewMgr.inst.ConnectToGameTz();
                })

            }else if(code === Respones.TZ_START_ERR){
                console.log('=========onGamePlayTzRes err===============',code)
            }else if(code == Respones.QIU_NAN){
                GameTip.inst.showTip('球币不足！');
            }else if(code == Respones.COUNT_TZ_NAN){
                GameTip.inst.showTip('今日挑战次数不足！');
            }
        }
    } 
}
